/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/* vim:set et sts=4: */
/* IBust - IBus with Traditional interface
 * Copyright(c) 2013 Daniel Sun
 * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
 * Copyright (C) 2008-2010 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <ibust.h>
#include <stdlib.h>
#include <locale.h>
#include "config-private.h"

static IBustBus *bus = NULL;
static IBustConfigDConf *config = NULL;

/* options */
static gboolean ibust = FALSE;
static gboolean verbose = FALSE;

static const GOptionEntry entries[] =
{
    { "ibust", 'i', 0, G_OPTION_ARG_NONE, &ibust, "component is executed by ibust",
      NULL },
    { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "verbose", NULL },
    { NULL },
};


static void
ibust_disconnected_cb (IBustBus  *bus,
                      gpointer  user_data)
{
    ibust_quit ();
}

static void
ibust_dconf_start (void)
{
    ibust_init ();
    bus = ibust_bus_new ();
    if (!ibust_bus_is_connected (bus)) {
        exit (1);
    }
    g_signal_connect (bus, "disconnected", G_CALLBACK (ibust_disconnected_cb),
                      NULL);
    config = ibust_config_dconf_new (ibust_bus_get_connection (bus));
    ibust_bus_request_name (bus, IBUST_SERVICE_CONFIG, 0);
    ibust_main ();
}

gint
main (gint argc, gchar **argv)
{
    GError *error = NULL;
    GOptionContext *context;

    setlocale (LC_ALL, "");
    g_setenv ("DCONF_PROFILE", "ibust", FALSE);

    context = g_option_context_new ("- ibust dconf component");

    g_option_context_add_main_entries (context, entries, "ibust-dconf");

    if (!g_option_context_parse (context, &argc, &argv, &error)) {
        g_print ("Option parsing failed: %s\n", error->message);
        exit (-1);
    }

    ibust_set_log_handler (verbose);
    ibust_dconf_start ();

    return 0;
}
